package com.cloud.base.thread.sync;

import lombok.SneakyThrows;

public class SynchronizeMain {
    private String name;

//    修饰代码块：可以自定义对象锁（类中的对象）
//    修饰静态代码块：相当于锁的是当前class类
//    修饰普通方法：相当锁的this，当前类对象
    public static void main(String[] args) throws InterruptedException {

        new Thread(() -> {
            add();
        }).start();
        Thread.sleep(1000);
        new Thread(() -> {
            synchronized (SynchronizeMain.class){
                System.out.println("synchronized执行开始："+Thread.currentThread().getName());
            }
        }).start();
    }

    @SneakyThrows
    public synchronized static void add() {
        System.out.println("add执行开始："+Thread.currentThread().getName());
        Thread.sleep(5000);
        System.out.println("add执行结束："+Thread.currentThread().getName());
    }
    @SneakyThrows
    public synchronized void del() {
        System.out.println("del执行开始:"+Thread.currentThread().getName());
        Thread.sleep(5000);
        System.out.println("del执行结束:"+Thread.currentThread().getName());
    }
}
